package com.uplift.mgt.product.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.uplift.common.result.IResultable;
import com.uplift.common.result.ResultWrapper4Layui;
import com.uplift.mgt.product.model.Product;
import com.uplift.mgt.product.service.IProductService;

/**
 * 商品相关
 * 
 * @author bcwang
 *
 */
@Controller
@RequestMapping(value = "/product")
public class ProductController {

	@Autowired
	private IProductService productService;

	/**
	 * 查询商品
	 * 
	 * @param user
	 * @return IResultable
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public IResultable list(HttpServletRequest request, Product product) {
		try {
			List<Map<String, Object>> list = this.productService.queryProduct(product);
			return ResultWrapper4Layui.wrapListPage(list, ResultWrapper4Layui.SUCC, "查询成功");
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapListPage(null, ResultWrapper4Layui.FAIL, "查询失败");
		}

	}
	/**
	 * 查询商品详细
	 * 
	 * @param user
	 * @return IResultable
	 */
	@RequestMapping(value = "/detail")
	@ResponseBody
	public IResultable detail(HttpServletRequest request, Product product) {
		try {
			Map<String, Object> map = this.productService.queryProductDetail(product);
			return ResultWrapper4Layui.wrapMap(map, ResultWrapper4Layui.SUCC, "查询成功");
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "查询失败");
		}

	}
	
	/**
	 * 新增商品
	 * 
	 * @param request
	 * @param Product
	 * @return IResultable
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public IResultable save(HttpServletRequest request, Product product) {
		try {
			int row = this.productService.saveProduct(product);
			if (row > 0) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "保存成功");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "保存失败");
			}
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "保存失败");
		}
	}

	/**
	 * 修改商品
	 * 
	 * @param request
	 * @param Product
	 * @return IResultable
	 */
	@RequestMapping(value = "/modify")
	@ResponseBody
	public IResultable modify(HttpServletRequest request, Product product) {
		try {
			int row = this.productService.modifyProduct(product);
			if (row > 0) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "修改成功");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "修改失败");
			}
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "修改失败");
		}
	}

	/**
	 * 删除商品
	 * 
	 * @param request
	 * @param Product
	 * @return IResultable
	 */
	@RequestMapping(value = "/remove")
	@ResponseBody
	public IResultable remove(HttpServletRequest request, Product product) {
		try {
			int row = this.productService.removeProduct(product);
			if (row > 0) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "删除成功");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "删除失败");
			}
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "删除失败");
		}
	}

	/**
	 * 发布商品
	 * 
	 * @param request
	 * @param Product
	 * @return IResultable
	 */
	@RequestMapping(value = "/publish")
	@ResponseBody
	public IResultable pub(HttpServletRequest request, Product product) {
		String pubStr = "";
		try {
			String status = product.getStatus();
			if (!StringUtils.equals(status, "1") && !StringUtils.equals(status, "0")) {
				status = "0";
				product.setStatus("0");
			}
			if (StringUtils.equals(status, "1")) {
				product.setPubTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				pubStr = "发布";
			} else {
				pubStr = "取消发布";
			}
			int row = this.productService.modifyProduct(product);
			if (row > 0) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, pubStr + "成功");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, pubStr + "失败");
			}
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, pubStr + "失败");
		}
	}
}
